#include"tcp_client.h"
#include<string.h>

// 设置路由分发机制
// typedef void msg_callback(const char *data, uint32_t len, int msgid, net_connection* net_conn, void *args);
void call_back1(const char *data, uint32_t len, int msgid, net_connection* net_conn, void *args){
    printf("call_back1: msgid:%d, msglen:%d, msg:%s", msgid, len, data);
    // net_conn->send_message(data, len, msgid);
}

void call_back2(const char *data, uint32_t len, int msgid, net_connection* net_conn, void *args){
    printf("call_back2: msgid:%d, msglen:%d, msg:%s", msgid, len, data);
    // net_conn->send_message(data, len, msgid);
}

// 设置创建链接和关闭连接的hook函数
// typedef void (*conn_callback)(net_connection *conn, void *args);
void on_client_build(net_connection *conn, void *args){
    int msgid = 2;
    const char* data = "I am coming!\n";
    conn->send_message(data, strlen(data), msgid);
}
void on_client_lost(net_connection *conn, void *args){
    printf("server are offline\n");
}

int main(){
    event_loop loop;
    //创建套接字
    tcp_client client(&loop,"127.0.0.1", 7777);
    
    // 注册一个回调业务
    client.add_msg_router(400, call_back1);
    client.add_msg_router(200, call_back2);

    client.set_conn_start(on_client_build);
    client.set_conn_close(on_client_lost);
    
    // 进入循环开始监听
    loop.event_process();

    return 0;
}
